import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtBaseMerchantStaffApi } from '#/api/cmt/base/merchantStaff';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getRangePickerDefaultProps } from '#/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        api: () => getBaseCommunityOptions(),
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择社区',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      rules: 'required',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        api: () => getMerchantInfoOptions(),
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择商户',
      },
    },
    {
      fieldName: 'userId',
      label: '会员ID',
      rules: 'required',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'staffName',
      label: '员工姓名',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入员工姓名',
      },
    },
    {
      fieldName: 'staffPhone',
      label: '员工电话',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入员工电话',
      },
    },
    {
      fieldName: 'role',
      label: '角色',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择角色',
        options: [
          { label: '普通员工', value: 1 },
          { label: '管理员', value: 2 },
          { label: '超级管理员', value: 3 },
        ],
      },
      defaultValue: 1,
    },
    {
      fieldName: 'permissions',
      label: '权限',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入权限(JSON数组)',
      },
    },
    {
      fieldName: 'joinTime',
      label: '加入时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择加入时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        class: '!w-full',
      },
    },
    {
      fieldName: 'staffStatus',
      label: '员工状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: [
          { label: '离职', value: 0 },
          { label: '在职', value: 1 },
        ],
      },
      defaultValue: 1,
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
      rules: z.number().default(CommonStatusEnum.ENABLE).optional(),
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      componentProps: {
        api: () => getBaseCommunityOptions(),
        labelField: 'name',
        valueField: 'id',
        clearable: true,
        placeholder: '请选择社区',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      componentProps: {
        api: () => getMerchantInfoOptions(),
        labelField: 'name',
        valueField: 'id',
        clearable: true,
        placeholder: '请选择商户',
      },
    },
    {
      fieldName: 'userId',
      label: '会员ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'staffName',
      label: '员工姓名',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入员工姓名',
      },
    },
    {
      fieldName: 'staffPhone',
      label: '员工电话',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入员工电话',
      },
    },
    {
      fieldName: 'role',
      label: '角色',
      component: 'Select',
      componentProps: {
        clearable: true,
        placeholder: '请选择角色',
        options: [
          { label: '普通员工', value: 1 },
          { label: '管理员', value: 2 },
          { label: '超级管理员', value: 3 },
        ],
      },
    },
    {
      fieldName: 'staffStatus',
      label: '员工状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        placeholder: '请选择员工状态',
        options: [
          { label: '离职', value: 0 },
          { label: '在职', value: 1 },
        ],
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'joinTime',
      label: '加入时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtBaseMerchantStaffApi.BaseMerchantStaff>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: 'ID',
      minWidth: 80,
    },
    {
      field: 'communityId',
      title: '社区ID',
      minWidth: 100,
    },
    {
      field: 'merchantId',
      title: '商户ID',
      minWidth: 100,
    },
    {
      field: 'userId',
      title: '会员ID',
      minWidth: 100,
    },
    {
      field: 'staffName',
      title: '员工姓名',
      minWidth: 120,
    },
    {
      field: 'staffPhone',
      title: '员工电话',
      minWidth: 120,
    },
    {
      field: 'role',
      title: '角色',
      minWidth: 120,
      slots: { default: 'role' },
    },
    {
      field: 'joinTime',
      title: '加入时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'staffStatus',
      title: '员工状态',
      minWidth: 100,
      slots: { default: 'staffStatus' },
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
